v = int(input())
s = []
s.append(v)
v = str(v)
f = []
if len(str(v)) != 4 or int(v) < 0:
    print(0)
else:
    for i in range(4):
        s.append(int(v[i]))
        if i != 3:
            s.append(int(v[i:i+2]))
        if i != 2:
            s.append(int(v[i:i + 3]))
    for i in s:
        for j in range(2, i):
            if i % j == 0:
                break
        else:
            f.append(i)
    if 1000 > int(v) or int(v) > 9999 or f == []:
        print(0)
    else:
        print(max(f))